/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RepositoryGraph.java
 *
 * Created on Mar 25, 2011, 11:53:57 AM
 */
package ifs.RnD.Graph;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ifs.RnD.svnRead.DisplayRepositoryTree;
import ifs.RnD.svnRead.Node;
import ifs.RnD.svnRead.Main;
import ifs.RnD.svnRead.Tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UnsupportedLookAndFeelException;
import org.tmatesoft.svn.core.SVNException;

/**
 *
 * @author anaklk
 */
public class RepositoryGraph extends javax.swing.JFrame {

     int x = 150, y = 30;
     static int MAX_SIZE = 1000000;
     Vertex V[] = new Vertex[MAX_SIZE];      //to hold all the vertices
     int size = 0;                           //No of vertices
     int noEdges = 0;                        //No of edges
     Edge adjList[] = new Edge[MAX_SIZE];    //Adjacency List
     // Variables declaration
     Vertex u;    // current node
     Vertex test;
     Point mousePoint;    //mouse point of selected node
     Dimension formSize;   //to keep the size of the form
     Vertex from; //selected node
     Vertex to;   //second selection
     Vertex center;
     mouseAction ma = new mouseAction();
     boolean click = true;
     DisplayRepositoryTree disRepoTree;
     Compiler com;
     Node rootNode;
     private Container contPane;
     //Main mainInstance;
     Tree tree;
     private Node currentNode;
     private Node backNode;
     BreadcrumVertex Brdcrm;
     public Favourite favourite;
     public ArrayList<String> viewURLs;
     String currentURL;
    ArrayList<String> searchNodes;
    Vector<String> searchContents; 

     /** Creates new form RepositoryGraph
      * @param drt
      */
     public RepositoryGraph(DisplayRepositoryTree drt, Favourite fav) {

          initComponents();
          jComboBox1.setEditable(true);
          searchNodes = new ArrayList<String>();
          searchContents = new Vector<String>();
          favourite = fav;
          contPane = getContentPane();
          contPane.setBackground(Color.WHITE);
          contPane.setLayout(null);
          disRepoTree = drt;
          viewURLs = new ArrayList<String>();
          currentURL = null;

          addComponentListener(new java.awt.event.ComponentAdapter() {

               @Override
               public void componentResized(java.awt.event.ComponentEvent evt) {
                    resized();
               }
          });
          formSize = getSize();
          this.setLayout(null);
     }

     /**
      *Resize the frame
      */
     public void resized() {
          formSize = getSize();
     }
     //Sets the using node as current node
     void setCurrentNode(Node nd) {
          this.currentNode = nd;
     }

     Node getCurrentNode() {
          return currentNode;
     }
     //Sets the forward node
     void setForwardNode(Node nd) {
          this.backNode = nd.getParent();

     }

     Node getForwardNode() {
          return backNode;
     }
     //sets the data to use in search area
     public void setSearchData(){
         searchNodes.clear();
         searchContents.clear();
         searchList.setListData(searchContents);
         searchField.setForeground(Color.GRAY);
         searchField.setText("Search .....");
         List list = currentNode.getChildren();
         Iterator iterator = list.iterator();
         while(iterator.hasNext()){
             Node node = (Node) iterator.next();
             searchNodes.add(node.getData());
         }
     }

     /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
     @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
     private void initComponents() {

          jPanel1 = new javax.swing.JPanel();
          jPanel2 = new javax.swing.JPanel();
          jPanel3 = new javax.swing.JPanel();
          jComboBox1 = new javax.swing.JComboBox();
          okButton = new javax.swing.JButton();
          jPanel4 = new javax.swing.JPanel();
          homeButton = new javax.swing.JButton();
          backButton = new javax.swing.JButton();
          fowardButton = new javax.swing.JButton();
          refreshButton = new javax.swing.JButton();
          saveButton = new javax.swing.JButton();
          helpButton = new javax.swing.JButton();
          jPanel5 = new javax.swing.JPanel();
          searchField = new javax.swing.JTextField();
          jScrollPane1 = new javax.swing.JScrollPane();
          searchList = new javax.swing.JList();
          searchButton = new javax.swing.JButton();
          jLabel3 = new javax.swing.JLabel();
          jLabel1 = new javax.swing.JLabel();

          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
          setTitle("Repository Graph");
          setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
          setMinimumSize(new java.awt.Dimension(1000, 600));
          addComponentListener(new java.awt.event.ComponentAdapter() {
               public void componentResized(java.awt.event.ComponentEvent evt) {
                    formComponentResized(evt);
               }
          });

          jPanel1.setBackground(new java.awt.Color(255, 255, 255));
          jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          jPanel1.setPreferredSize(new java.awt.Dimension(1000, 790));

          javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
               jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGap(0, 998, Short.MAX_VALUE)
          );
          jPanel1Layout.setVerticalGroup(
               jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGap(0, 788, Short.MAX_VALUE)
          );

          jPanel2.setBackground(new java.awt.Color(102, 102, 102));
          jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
          jPanel2.setPreferredSize(new java.awt.Dimension(1033, 35));

          javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
          jPanel2.setLayout(jPanel2Layout);
          jPanel2Layout.setHorizontalGroup(
               jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGap(0, 1016, Short.MAX_VALUE)
          );
          jPanel2Layout.setVerticalGroup(
               jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGap(0, 31, Short.MAX_VALUE)
          );

          jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          jPanel3.setPreferredSize(new java.awt.Dimension(1023, 40));

          jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 12));
          jComboBox1.setPreferredSize(new java.awt.Dimension(600, 25));
          jComboBox1.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jComboBox1ActionPerformed(evt);
               }
          });

          okButton.setBackground(new java.awt.Color(102, 102, 102));
          okButton.setFont(new java.awt.Font("Tahoma", 0, 13));
          okButton.setText("OK");
          okButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
          okButton.setFocusable(false);
          okButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
          okButton.setMaximumSize(new java.awt.Dimension(60, 21));
          okButton.setMinimumSize(new java.awt.Dimension(50, 21));
          okButton.setPreferredSize(new java.awt.Dimension(90, 25));
          okButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
          okButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    okButtonActionPerformed(evt);
               }
          });

          javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
          jPanel3.setLayout(jPanel3Layout);
          jPanel3Layout.setHorizontalGroup(
               jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(266, Short.MAX_VALUE))
          );
          jPanel3Layout.setVerticalGroup(
               jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );

          jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          jPanel4.setPreferredSize(new java.awt.Dimension(140, 470));

          homeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/home.png"))); // NOI18N
          homeButton.setToolTipText("Root Repository");
          homeButton.setPreferredSize(new java.awt.Dimension(80, 70));
          homeButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    homeButtonActionPerformed(evt);
               }
          });

          backButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/folder_previous.png"))); // NOI18N
          backButton.setToolTipText("Previous Folder");
          backButton.setPreferredSize(new java.awt.Dimension(80, 70));
          backButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    backButtonActionPerformed(evt);
               }
          });

          fowardButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/folder_next.png"))); // NOI18N
          fowardButton.setToolTipText("Forward Folder");
          fowardButton.setPreferredSize(new java.awt.Dimension(80, 70));
          fowardButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fowardButtonActionPerformed(evt);
               }
          });

          refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/Refresh.png"))); // NOI18N
          refreshButton.setToolTipText("Refresh");
          refreshButton.setPreferredSize(new java.awt.Dimension(80, 70));
          refreshButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    refreshButtonActionPerformed(evt);
               }
          });

          saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/Save.png"))); // NOI18N
          saveButton.setToolTipText("Save Path");
          saveButton.setPreferredSize(new java.awt.Dimension(80, 70));
          saveButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    saveButtonActionPerformed(evt);
               }
          });

          helpButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/help.png"))); // NOI18N
          helpButton.setToolTipText("Help");
          helpButton.setPreferredSize(new java.awt.Dimension(80, 70));
          helpButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    helpButtonActionPerformed(evt);
               }
          });

          javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
          jPanel4.setLayout(jPanel4Layout);
          jPanel4Layout.setHorizontalGroup(
               jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                         .addComponent(homeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(helpButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(fowardButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(30, Short.MAX_VALUE))
          );
          jPanel4Layout.setVerticalGroup(
               jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(homeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(fowardButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(helpButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );

          jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          jPanel5.setPreferredSize(new java.awt.Dimension(140, 210));

          searchField.setText("Search .....");
          searchField.setPreferredSize(new java.awt.Dimension(90, 25));
          searchField.addCaretListener(new javax.swing.event.CaretListener() {
               public void caretUpdate(javax.swing.event.CaretEvent evt) {
                    searchFieldCaretUpdate(evt);
               }
          });
          searchField.addFocusListener(new java.awt.event.FocusAdapter() {
               public void focusGained(java.awt.event.FocusEvent evt) {
                    searchFieldFocusGained(evt);
               }
          });

          jScrollPane1.setPreferredSize(new java.awt.Dimension(120, 200));

          searchList.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
          jScrollPane1.setViewportView(searchList);

          searchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/searchw.png"))); // NOI18N
          searchButton.setToolTipText("Search");
          searchButton.setPreferredSize(new java.awt.Dimension(25, 25));
          searchButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    searchButtonActionPerformed(evt);
               }
          });

          javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
          jPanel5.setLayout(jPanel5Layout);
          jPanel5Layout.setHorizontalGroup(
               jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel5Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                              .addComponent(searchField, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
          );
          jPanel5Layout.setVerticalGroup(
               jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanel5Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addContainerGap())
          );

          jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
          jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
          jLabel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
          jLabel3.setPreferredSize(new java.awt.Dimension(140, 80));

          jLabel1.setFont(new java.awt.Font("Tw Cen MT Condensed", 1, 18));
          jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
          jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/logo.jpg"))); // NOI18N
          jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
          jLabel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
          jLabel1.setPreferredSize(new java.awt.Dimension(140, 80));
          jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
               layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                              .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                              .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                              .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, 0, 140, Short.MAX_VALUE)
                              .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                              .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE))
                         .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 1020, Short.MAX_VALUE)
                         .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 1020, Short.MAX_VALUE))
                    .addContainerGap())
          );
          layout.setVerticalGroup(
               layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                         .addGroup(layout.createSequentialGroup()
                              .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                              .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(7, 7, 7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(layout.createSequentialGroup()
                              .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addGap(12, 12, 12)
                              .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
          );

          pack();
     }// </editor-fold>//GEN-END:initComponents

     private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

          String newURL = jComboBox1.getSelectedItem().toString();
          try {
               checkRoot(newURL);
          } catch (SVNException ex) {
               Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
          }


     }//GEN-LAST:event_okButtonActionPerformed
     //When the component resize
     private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized

          jPanel1.setSize(this.getWidth() - 170, this.getHeight() - 130);
          jPanel2.setSize(this.getWidth() - 170, jPanel2.getHeight());
          jPanel3.setSize(this.getWidth() - 170, jPanel3.getHeight());
          jComboBox1.setSize(this.getWidth() - 200, jComboBox1.getHeight());
          jLabel3.setSize(jLabel3.getWidth(), jPanel1.getHeight() - 700);
          drawGraph(currentNode);
     }//GEN-LAST:event_formComponentResized

     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed

     }//GEN-LAST:event_jMenuItem2ActionPerformed

     private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
          if (currentNode.getParent() == null) {
               JOptionPane.showMessageDialog(rootPane, "This is root");
          } else {
               drawGraph(currentNode.getParent());
          }          // TODO add your handling code here:
     }//GEN-LAST:event_backButtonActionPerformed
     //Forward the selected URL from combo box
     private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
          // TODO add your handling code here:
          JComboBox cb = (JComboBox) evt.getSource();
          String nodeURL = (String) cb.getSelectedItem();
          if (!currentURL.equals(nodeURL)) {
               try {
                    checkRoot(nodeURL);
               } catch (SVNException ex) {
                    Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
               }
          }

     }//GEN-LAST:event_jComboBox1ActionPerformed
     //Draws the root when Home button action performed.
     private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
          drawGraph(disRepoTree.directTree.getRootElement());
     }//GEN-LAST:event_homeButtonActionPerformed
         //Draws the previous when back button action performed.
     private void fowardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fowardButtonActionPerformed
          if (backNode == null) {
               JOptionPane.showMessageDialog(rootPane, "Can't go forward");
          } else {

               drawGraph(backNode);
          }
     }//GEN-LAST:event_fowardButtonActionPerformed
     //Draws the root when home button action performed.
     private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed

          String url = jComboBox1.getSelectedItem().toString();
          List list = (List) disRepoTree.directTree.toList();
          Iterator iterator = list.iterator();
          while (iterator.hasNext()) {
               Node node = (Node) iterator.next();
               String url1 = node.getSvnURL();

               if (url.equals(url1)) {
                    node.getChildren().clear();

                    this.setForwardNode(node);
                    if (node.getNumberOfChildren() == 0 && node.getType().equalsIgnoreCase("DIR")) {
                         disRepoTree.cerateRepoToURL(node.getSvnURL());
                         try {
                              Node root = disRepoTree.readRepository(disRepoTree.getRepository(), "");
                         } catch (SVNException ex) {
                              Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
                         }
                         if (node.getNumberOfChildren() == 0) {
                         } else {
                              this.drawGraph(node);
                         }
                    } else if (node.getType().equalsIgnoreCase("FILE")) {
                    } else if (node.getParent() == null) {
                    } else if (this.rootNode == node) {
                         this.drawGraph(node.getParent());
                         this.setVisible(true);
                    } else {
                         this.drawGraph(node);
                         this.setVisible(true);
                    }
               }
          }          
     }//GEN-LAST:event_refreshButtonActionPerformed
     //call save action when save button action performed 
     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
          String fav;
          fav = jComboBox1.getSelectedItem().toString();
          try {
               favourite.save(fav);
          } catch (Exception e) {
          }
          JOptionPane.showMessageDialog(rootPane, "URL saved", "Favourites", 1);          // TODO add your handling code here:
     }//GEN-LAST:event_saveButtonActionPerformed
     //Shows the help form when help button action performed
     private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
          new Helpform().setVisible(true);
     }//GEN-LAST:event_helpButtonActionPerformed
     //Starts the search process when search button action performed
     private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
         String keyword = null;
        try{
        keyword = searchList.getSelectedValue().toString();
            try {
                u.search(keyword);
            } catch (SVNException ex) {
                Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
            }
        }catch(NullPointerException e){
        keyword = searchField.getText();
            try {
                u.search(keyword);
            } catch (SVNException ex) {
                Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     }//GEN-LAST:event_searchButtonActionPerformed
     //Change the text in search text field when search field focus gained
     private void searchFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchFieldFocusGained
         searchField.setForeground(Color.BLACK);
         if(searchField.getText().equals("Search .....")){
             searchField.setText("");    
         }
     }//GEN-LAST:event_searchFieldFocusGained
     //Update the search list when character update in the search field
     private void searchFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_searchFieldCaretUpdate
         String searchName = searchField.getText();
        searchContents.clear();
        if(searchField.getText().equals("")){
            searchList.setListData(searchContents);
        }else{
        for(int i=0; i<searchNodes.size(); i++){
            if((searchNodes.get(i).toLowerCase()).startsWith(searchName.toLowerCase())){
                searchContents.add(searchNodes.get(i));
            }
            
        }
        searchList.setListData(searchContents);
        
     }                                       

            
     }//GEN-LAST:event_searchFieldCaretUpdate
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton backButton;
     private javax.swing.JButton fowardButton;
     private javax.swing.JButton helpButton;
     private javax.swing.JButton homeButton;
     private javax.swing.JComboBox jComboBox1;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel3;
     private javax.swing.JPanel jPanel1;
     private javax.swing.JPanel jPanel2;
     private javax.swing.JPanel jPanel3;
     private javax.swing.JPanel jPanel4;
     private javax.swing.JPanel jPanel5;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JButton okButton;
     private javax.swing.JButton refreshButton;
     private javax.swing.JButton saveButton;
     private javax.swing.JButton searchButton;
     private javax.swing.JTextField searchField;
     private javax.swing.JList searchList;
     // End of variables declaration//GEN-END:variables

     /**
      * sets the positions of nodes and edges of this node and it's children nodes
      * @param node
      */
     public void drawGraph(Node node) {

          setCurrentNode(node);
          setSearchData();
          currentURL = node.getSvnURL();
          jPanel1.removeAll();
          jPanel2.removeAll();
          rootNode = node;
          setURL(rootNode.getSvnURL());
          int n = rootNode.getNumberOfChildren();
          int radiusY, radiusX;
          drawBreadcrum(node);

          radiusY = (int) (jPanel1.getHeight() / 2 - 50);
          radiusX = (int) (jPanel1.getWidth() / 2 - 50);
          if (rootNode.getType().equals("ROOT")) {
               center = addVertex(rootNode.getData(), rootNode.getSvnURL(), (jPanel1.getWidth() / 2 - 25), (jPanel1.getHeight() / 2), rootNode.getType());
          } else if (rootNode.getType().equals("DIR")) {
               center = addVertex(rootNode.getData(), rootNode.getSvnURL(), (jPanel1.getWidth() / 2 - 25), (jPanel1.getHeight() / 2), "midle");
          }
          if (n <= 15) {
               for (int i = 0; i < n; i++) {
                    Node tempNode = rootNode.getChildren().get(i);
                    Vertex tempVertex;
                    tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((1.00 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((1.00 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                    addEdge(center, tempVertex, jPanel1);
               }
          } else if (n > 15 && n <= 35) {
               for (int i = 0; i < n; i++) {
                    Node tempNode = rootNode.getChildren().get(i);
                    Vertex tempVertex;
                    if (i % 2 == 0) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((1.00 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((1.00 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.80 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.80 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    }
               }
          } else if (n > 35) {
               for (int i = 0; i < n; i++) {
                    Node tempNode = rootNode.getChildren().get(i);
                    Vertex tempVertex;
                    if (i % 7 == 0) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((1.00 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((1.00 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else if (i % 7 == 1) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.90 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.90 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else if (i % 7 == 2) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.80 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.80 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else if (i % 7 == 3) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.70 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.70 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else if (i % 7 == 4) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.60 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.60 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else if (i % 7 == 5) {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.50 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.45 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    } else {
                         tempVertex = addVertex(tempNode.getData(), tempNode.getSvnURL(), (int) ((0.30 * radiusX * Math.sin(i * 2 * Math.PI / n) + jPanel1.getWidth() / 2) - 25), (int) ((0.30 * radiusY * Math.cos(i * 2 * Math.PI / n) + jPanel1.getHeight() / 2)), tempNode.getType());
                         addEdge(center, tempVertex, jPanel1);
                    }
               }
          }
     }

     //Set URLs in the combo box
     public void setURL(String url) {
          viewURLs.clear();
          viewURLs.add(url);
          jComboBox1.setModel(new javax.swing.DefaultComboBoxModel((favourite.readFileForView(viewURLs)).toArray()));

     }
     //Check the root of the selected URL
     public void checkRoot(String nodeURL) throws SVNException {
          String rootUrl = disRepoTree.directTree.getRootElement().getSvnURL();
          int urlLeangth = rootUrl.length();
          String subURL = null;
          if (nodeURL.length() < urlLeangth) {
               try {
                    String newURL = jComboBox1.getSelectedItem().toString();
                    Main main = new Main(newURL, 2);
                    main.setURL();
                    this.dispose();
                    main.setVisible(true);

               } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
               } catch (ParseException ex) {
                    Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
               }
          } else {
               subURL = nodeURL.substring(0, urlLeangth);

               if (rootUrl.equals(subURL)) {
                    if (!disRepoTree.readExternals(nodeURL)) {
                         disRepoTree = new DisplayRepositoryTree(nodeURL, disRepoTree.getUserName(), disRepoTree.getPassword(), disRepoTree.getMain(), favourite);
                         disRepoTree.processLink(nodeURL, disRepoTree, this);
                    }
               } else {
                    try {
                         String newURL = jComboBox1.getSelectedItem().toString();
                         Main main = new Main(newURL, 2);
                         main.setURL();
                         this.dispose();
                         main.setVisible(true);

                    } catch (UnsupportedLookAndFeelException ex) {
                         Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ParseException ex) {
                         Logger.getLogger(RepositoryGraph.class.getName()).log(Level.SEVERE, null, ex);
                    }
               }
          }
     }

     /**
      * Creates a new Vertex Object and adds actions to the created Vertex label
      * @param node
      */
     public void drawBreadcrum(Node node) {
          String root = disRepoTree.getRootURL();
          String relPath = node.getSvnURL().substring(root.lastIndexOf("/") + 1);
          String comp[] = relPath.split("/");
          String svnUrl = root;

          for (int i = 0; i < comp.length; i++) {
               System.out.println("comp  :  " + comp[i]);
               if (i != 0) {
                    svnUrl = svnUrl + "/" + comp[i];
               }
               BreadcrumVertex tempBVertex = addBreadscrumComp(comp[i], svnUrl, jPanel2.getX() + 90 * i - 145, jPanel2.getY() - 30);
          }
     }

     /**
      * Creates a new Vertex Object and adds actions to the created Vertex label
      * @param title
      * @param url
      * @param dx
      * @param dy
      * @param type
      * @return Vertex
      */
     public Vertex addVertex(String title, String url, int dx, int dy, String type) {
          u = new Vertex(title, url, this, disRepoTree, type);
          V[size++] = u;
          u.addMouseListener(ma);
          u.addMouseMotionListener(ma);
          jPanel1.add(u);
          if (type.equals("ROOT") || type.equals("midle")) {
               u.setBounds(x = x + 50, y = y + 50, 100, 100);
          } else {
               u.setBounds(x = x + 70, y = y + 40, 98, 45);
          }
          u.setLocation(dx - 25, dy - 25);
          return u;

     }

     /**
      * Creates a new Vertex Object and adds actions to the created Vertex label
      * @param title
      * @param url
      * @param dx
      * @param dy
      * @return BreadcrumVertex
      */
     public BreadcrumVertex addBreadscrumComp(String title, String url, int dx, int dy) {
          Brdcrm = new BreadcrumVertex(title, url, this, disRepoTree);
          // Brdcrm.addMouseListener(ma);
          jPanel2.add(Brdcrm);
          Brdcrm.setBounds(x = x + 50, y = y + 50, 90, 40);
          Brdcrm.setLocation(dx - 25, dy - 25);
          return Brdcrm;

     }

     /**
      * Creates a new edge object in between coming Vertex labels and adds that to the panel
      * @param f
      * @param t
      * @param panel
      */
     public void addEdge(Vertex f, Vertex t, JPanel panel) {
          from = null;
          Edge e = adjList[f.number];
          Edge n = new Edge(f, t);
          noEdges++;
          panel.add(n);
          n.repaint();
          if (e == null) {
               adjList[f.number] = n;
          } else {
               e.next = n;
          }
          n.start = f;
          n.end = t;
          // Shifting the edge
          e = adjList[t.number];
          while (e != null) {
               if (e.end == f) {
                    e.shift();
                    n.shift();
                    return;
               }
               if (e.next == null) {
                    break;
               }
               e = e.next;
          }
     }

     /**
      *
      * @param c
      */
     public void click(boolean c) {
          click = c;
     }

     public JLabel getLable() {
          return jLabel3;
     }

     class mouseAction extends MouseAdapter implements MouseMotionListener {

          @Override
          public void mousePressed(java.awt.event.MouseEvent evt) {
               u = (Vertex) evt.getComponent();
               mousePoint = evt.getPoint();
          }

          @Override
          public void mouseClicked(MouseEvent evt) {
               if (!click) {
                    return;
               }
               if (from == null) {
                    from = (Vertex) evt.getComponent();
                    from.select();
               } else {
                    from.unselect();
                    to = (Vertex) evt.getComponent();
               }
          }

          @Override
          public void mouseDragged(MouseEvent evt) {
               //Code to handel vertex movement
               double getx1 = jPanel1.getLocation().getX();
               double gety1 = jPanel1.getLocation().getY();
               double widthPanl = jPanel1.getWidth();
               double heightPanl = jPanel1.getHeight();

               int x = u.getX() + evt.getX() - mousePoint.x;
               int y = u.getY() + evt.getY() - mousePoint.y;
               if ((x <= (getx1 + widthPanl - 250)) && (y <= (gety1 + heightPanl - 135))) {

                    if (x < 0) {
                         x = 0;
                    }
                    if (y < 0) {
                         y = 0;
                    }
                    if (x > formSize.width - 60) {
                         x = formSize.width - 60;
                    }
                    if (y > formSize.height - 80) {
                         y = formSize.height - 80;
                    }
                    u.setLocation(x, y);
               }
          }
     }
}
